<template>
  <div>
    <el-dialog :title="info.isAdd ? '添加' : '编辑'" :visible.sync="info.isShow" @close="close">
      <div>{{ user }}</div>
      <el-form :model="user" label-width="80px">
        <el-form-item label="角色名称">
          <el-input v-model="user.rolename"></el-input>
        </el-form-item>
        <el-form-item label="角色权限">
          <el-tree
            :data="treeData"
            :props="{ children: 'children', label: 'title' }"
            node-key="id"
            ref="tree"
            show-checkbox
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="info.isAdd" type="primary" @click="add"
          >添 加</el-button
        >
        <el-button v-else type="primary" @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {
  reqmenulist,
  reqroleadd,
  reqroleedit,
  reqroleinfo,
} from "../../../http/api";
import { successAlert } from "../../../utils/alert";
import {formClose} from '../../../mixins/index'
export default {
  mixins:[formClose],
  props: ["info"],
  data() {
    return {
      user: {
        rolename: "",
        menus: [],
        status: 1,
      },
      //树形控件数据
      treeData: [],
    };
  },
  computed: {
    ...mapGetters({userInfo:'userInfo'}),
  },
  mounted() {
    this.getTreeData();
  },
  methods: {
    ...mapActions({changeUserInfo:'changeUserInfo'}),
    //获取树形菜单数据列表
    async getTreeData() {
      let { data: res } = await reqmenulist({ istree: true });
      if (res.code == 200) {
        this.treeData = res.list ? res.list : [];
      }
    },
    //清空表单
    empty() {
      this.user = {
        rolename: "",
        menus: [],
        status: 1,
      };
      //清除树的数据
      this.$refs.tree.setCheckedKeys([]);
    },
    //获取一条用户信息
    async getOne(id) {
      let { data: res } = await reqroleinfo({ id });
      if (res.code == 200) {
        this.user = res.list;
        this.user.id = id;
        this.$refs.tree.setCheckedKeys(JSON.parse(this.user.menus));
      }
    },
    //添加
    async add() {
      this.user.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      let { data: res } = await reqroleadd(this.user);
      if (res.code == 200) {
        successAlert(res.msg);
        //添加成功关闭弹窗
        this.info.isShow = false;
        //清空数据
        this.empty();
        //重新请求列表页数据
        this.$emit("init");
      }
    },
    //修改
    async update() {
      this.user.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      console.log(this.user);
      let { data: res } = await reqroleedit(this.user);
      if (res.code == 200) {
        successAlert(res.msg);
        //判断修信息是否影响当前用户
        if(this.user.id==this.userInfo.roleid){
            this.changeUserInfo({})
            this.$router.push("/login")
            return;
          }
        //添加成功关闭弹窗
        this.info.isShow = false;
        //清空数据
        this.empty();
        //重新请求列表页数据
        this.$emit("init");
      }
    },
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>